package javaCode.chapter13.set_;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author 徐州城
 * @version 1.0
 */
@SuppressWarnings("all")
public class HashSetExercise02 {
    public static void main(String[] args) {
        HashSet hashSet = new HashSet();
        hashSet.add(new Employee_("张三",10000,new MyDate(2021,8,5)));
        hashSet.add(new Employee_("李四",20000,new MyDate(2021,8,5)));
        hashSet.add(new Employee_("张三",10000,new MyDate(2021,8,5)));
        hashSet.add(new Employee_("李四",20000,new MyDate(2021,8,5)));
        System.out.println("hashSet=" + hashSet);
    }
}

/*
顶一个Employee类，该类包含：private成员属性 name,sal,birthday(MyDate类型)，
其中 birthday 为 MyDate类型(属性包括：year，month，day)，要求：
1. 创建3个Employee 放入 HashSet中
2. 当 name和birthday的值相同时，认为相同员工，不能添加到HashSet集合中
 */

class Employee_{
    private String name;
    private double sal;
    private MyDate birthday;

    public Employee_(String name, double sal, MyDate birthday) {
        this.name = name;
        this.sal = sal;
        this.birthday = birthday;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee_ employee_ = (Employee_) o;
        return Objects.equals(name, employee_.name) &&
                Objects.equals(birthday, employee_.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }

    @Override
    public String toString() {
        return "Employee_{" +
                "name='" + name + '\'' +
                ", sal=" + sal +
                ", birthday=" + birthday +
                '}';
    }
}

class MyDate{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyDate myDate = (MyDate) o;
        return year == myDate.year &&
                month == myDate.month &&
                day == myDate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }
}